<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LY自动表单填写</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background: linear-gradient(45deg, #000000, #1a1a1a, #000000);
            background-size: 300% 300%;
            animation: gradient 30s ease infinite;
            color: #00ff80;
            overflow: auto; /* 允许滚动 */
            position: relative;
        }
        @keyframes gradient {
            0% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
            100% { background-position: 0% 50%; }
        }
        .container {
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
            background-color: rgba(0, 0, 0, 0.8);
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 255, 128, 0.5);
            position: relative;
            z-index: 10;
            overflow: auto; /* 允许滚动 */
            max-height: 80vh; /* 设置最大高度 */
        }
        .logo {
            display: block;
            margin: 0 auto 20px;
            width: 100px;
            height: auto;
        }
        button {
            margin: 10px 0;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            background-color: #00ff80;
            color: #000;
            border: none;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #00ff40;
        }
        .double-column {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }
        .card {
            background-color: rgba(0, 0, 0, 0.7);
            padding: 10px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 255, 128, 0.5);
            animation: fadeIn 1s ease-in-out;
        }
        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
        }
        .pagination button {
            margin: 0 5px;
        }
    </style>
</head>
<body>
<div class="container">
    <img src="/img/ly.png" alt="Logo" class="logo">
    <h1>LY自动化</h1>
    <button id="initializeButton">初始化</button>
    <button id="openButton">打开</button>
    <div id="dataContainer"></div>
    <div id="paginationContainer" class="pagination"></div>
</div>

<script>
    const initializeButton = document.getElementById('initializeButton');
    const openButton = document.getElementById('openButton');
    const dataContainer = document.getElementById('dataContainer');
    const paginationContainer = document.getElementById('paginationContainer');

    let currentPage = 1;
    const itemsPerPage = 10; // 每页显示的项目数
    let dataList = [];

    initializeButton.addEventListener('click', () => {
        fetch('/excel/initialize', {
            method: 'GET'
        })
            .then(response => response.json())
            .then(data => {
                if (data) {
                    return fetch('/excel/list', {
                        method: 'GET'
                    });
                } else {
                    throw new Error('初始化失败，未获取到数据');
                }
            })
            .then(response => response.json())
            .then(data => {
                dataList = data;
                renderData(dataList);
                alert('初始化成功');
            })
            .catch(error => {
                alert('初始化失败: ' + error.message);
            });
    });

    openButton.addEventListener('click', () => {
        fetch('/excel/open', {
            method: 'GET'
        })
            .then(response => response.text())
            .then(data => {
                alert('打开结果: ' + data);
            })
            .catch(error => {
                alert('打开失败: ' + error);
            });
    });

    function renderData(dataList) {
        const table = document.createElement('div');
        table.className = 'double-column';

        const start = (currentPage - 1) * itemsPerPage;
        const end = start + itemsPerPage;
        const pageData = dataList.slice(start, end);

        pageData.forEach((item, index) => {
            const card = document.createElement('div');
            card.className = 'card';
            card.innerHTML = `
                <p><strong>姓名：</strong>${item.name}</p>
                <p><strong>证件号码：</strong>${item.idNumber}</p>
                <p><strong>手机号：</strong>${item.phoneNumber}</p>
            `;
            table.appendChild(card);
        });

        dataContainer.innerHTML = '';
        dataContainer.appendChild(table);

        renderPagination(dataList.length);
    }

    function renderPagination(totalItems) {
        const totalPages = Math.ceil(totalItems / itemsPerPage);
        paginationContainer.innerHTML = '';

        for (let i = 1; i <= totalPages; i++) {
            const button = document.createElement('button');
            button.textContent = i;
            button.addEventListener('click', () => {
                currentPage = i;
                renderData(dataList); // 重新渲染数据
            });
            paginationContainer.appendChild(button);
        }
    }
</script>
</body>
</html>
